package com.mingqi.enetconnected.net.request;

import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.mingqi.enetconnected.net.model.app.CheckVersionInfo;
import com.mingqi.enetconnected.net.util.HttpParameters;
import com.mingqi.enetconnected.util.AppUtils;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.util.Tuple;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.lang.reflect.Type;

public class AppServerManagement {
    /**
     * 版本检测
     */
    public final static String CHECK_VERSION_COMMAND = "CK";

    /**
     * 版本检测
     *
     * @param cxt
     * @return null不需更新；<下载地址，版本说明>
     */
    public static Tuple<String, String> checkVersion(Context cxt) {
        Type type = new TypeToken<CheckVersionInfo>() {
        }.getType();
        JSONObject jsondata = new JSONObject();
        try {

            jsondata.put("FormatType", "0");
            jsondata.put("DeviceID", "864323022536560");
            jsondata.put("DATA", "android" + HttpParameters.VALUE_SPLIT + AppUtils.getAppVersion(cxt));
            jsondata.put("Command", CHECK_VERSION_COMMAND);
            jsondata.put("FileID", "");
            jsondata.put("FileName", "");
            jsondata.put("EmployeeID", "1001");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String info = "?TransData=" + jsondata.toString();
        Communicator<CheckVersionInfo> communicator = new Communicator<>(cxt, HttpParameters.APP_SERVER_COMMAND_URL + info);
        try {

            CheckVersionInfo checkVersionInfo = communicator.post(type);
            String[] buffers = checkVersionInfo.getBuffer().split(String.format("%c", HttpParameters.VALUE_SPLIT));
            if (!checkVersionInfo.getBuffer().isEmpty() && buffers.length != 2) {
                //"lastver", "downloadurl","lastverlength", "verInfo"
                // String buffer = getBufferData(checkVersionInfo.getBuffer());
                String[] values = checkVersionInfo.getBuffer().split(String.format("%c", HttpParameters.STRING_SPLIT));
                if (values.length == 4) {
                    return new Tuple<>(values[1], values[3]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 下载新版本
     *
     * @param cxt
     * @param url
     * @param fileCallback
     */
    public static void downloadApk(Context cxt, String url, FileCallback fileCallback) {
        if (!url.startsWith("http")) {
            url = HttpParameters.APP_SERVER_DOWNLOAD_URL + url;
        }
        OkGo.<File>get(url).tag(cxt).execute(fileCallback);
    }

    public static void login(Context cxt) {
        JSONObject json = new JSONObject();
        try {
            json.put("name", "终端登录");
            json.put("EmployeeID", "1001");
            json.put("Password", "1001");
            json.put("IMEI", "864323022536560");
            JSONObject othApp = new JSONObject();
            othApp.put("EquipmentModel", DeviceUtils.getSysModel());
            othApp.put("SysVersion", DeviceUtils.getSysVersion());
            othApp.put("RAM", "");
            othApp.put("Storage", "");
            othApp.put("CPU", "");
            othApp.put("OpenGL", "");
            othApp.put("NetworkStandard", "");
            json.put("OthApp", othApp);

        } catch (JSONException e) {
            e.printStackTrace();
        }
//        Communicator<String> communicator = new Communicator<>(cxt, HttpParameters.APP_SERVER_LABEL_URL);
//        try {
//            String value = communicator.post(json);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }


}
